package com.example.e_comm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.entity.Role;
import com.example.e_comm.service.PermissionService;
import com.example.e_comm.service.RoleService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/roles")
public class RoleController {
    private RoleService roleService;
    private PermissionService permissionService;
    private RoleController(RoleService _roleService
            , PermissionService  _permissionService){
        this.roleService = _roleService;
        this.permissionService = _permissionService;
    }
    @RequestMapping("/list")
    public List<Role> list(){
        return roleService.list();
    }
    @GetMapping("/page")
    public IPage<Role> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ){
        return roleService.page(new Page<>(pageNum, pageSize));
    }
    @GetMapping("/search")
    public IPage<Role> search(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword
    ){
        Page<Role> page = new Page<>(pageNum, pageSize);
        return roleService.page(new LambdaQueryWrapper< Role>()
                .like(Role::getName, keyword)
                .or()
                .like(Role::getDescription, keyword)
                .or()
                .like(Role::getRolekdy, keyword)
        );
    }
    @PostMapping("/create")
    public Role createRole(@RequestBody Role entity) {
        if (roleService.getById(entity.getId()) != null) {
            throw new IllegalArgumentException("角色已存在!");
        }
        roleService.save(entity);
        return entity;
    }
    @PostMapping("/update")
    public Role updateRole(@RequestBody Role entity) {
        if (roleService.getById(entity.getId()) != null) {
            throw new IllegalArgumentException("角色不存在!");
        }
        roleService.updateById(entity);
        return entity;
    }
    @PostMapping("/delete/{roleId}")
    public void deleteRole(@PathVariable Long roleId) {
        if (roleService.getById(roleId) == null) {
            throw new IllegalArgumentException("角色不存在!");
        }
        roleService.removeById(roleId);
    }
    @PostMapping("/deleteBatch")
    public void deleteRole(@RequestBody List<Long> roleIds) {
        List<Role> roles = roleService.listByIds(roleIds);
        if (roles.size() != roleIds.size()) {
            throw new IllegalArgumentException("角色有误，请核查！");
        }
        roleService.removeByIds(roleIds);
    }
    //获取角色权限列表
    @GetMapping("/getPermission")
    public List<Permission> getPermissionsByRoleId(Long roleId) {
        return permissionService.getPermissionsByRoleId(roleId);
    }
    //为角色分配权限
    @PostMapping("/assignPermissions")
    public void assignPermissions(
            @RequestParam Long roleId,
            @RequestParam List<Long> permissionIds
    ){
        //角色是否存在
        Role role = roleService.getById(roleId);
        if(role == null){
            throw new IllegalArgumentException("角色不存在！");
        }
        List<Permission> permissions = permissionService.listByIds(permissionIds);
        if(permissions.size() != permissionIds.size()){
            throw new IllegalArgumentException("请核查权限后再操作");
        }
        roleService.assignPermissions(roleId, permissionIds);
    }

}
